﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using BEGDC.Utility.ExceptionManagement;

namespace BEGDC.CC.Web.Workflow
{
	/// <summary>
	/// Summary description for Error.
	/// </summary>
	public class Error : System.Web.UI.Page
	{
		protected System.Web.UI.HtmlControls.HtmlGenericControl spnDetail;
		protected System.Web.UI.HtmlControls.HtmlGenericControl spnErrorMsg;
		protected System.Web.UI.HtmlControls.HtmlAnchor lnkDetail;
		protected System.Web.UI.HtmlControls.HtmlAnchor lnkGoBack;

		public static string SessionName = "NewException";
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			string error = Request.QueryString["error"];
			string msg = error;//BEGDC.CC.Resource.ErrorMessage.Keys.GetString( error.Replace(".","_") );
			if( msg == null || msg.Trim() == string.Empty )
				msg = "<b>[ "+error+" ]</b>";
			this.spnErrorMsg.InnerHtml = msg;

			BEGDC.Utility.ExceptionManagement.GDCException exp = Session[ Workflow.Error.SessionName ] as BEGDC.Utility.ExceptionManagement.GDCException;
			if( exp == null || exp.InnerException == null )
			{
				this.lnkDetail.Visible = false;
			}
			else
			{
				string detail = "[ " + exp.Source + " ] "
							+ " <b>" + exp.InnerException.Message +"</b>";

				this.spnDetail.InnerHtml = detail + "<br><br>" + exp.StackTrace;

				this.lnkDetail.Visible = true;
			}
			Session[ Workflow.Error.SessionName ] = null;	
		
			this.lnkGoBack.Title = "Back";
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	
		public static void CatchPopupError( System.Web.UI.Page page, GDCException exception )
		{
			page.Session[ Workflow.Error.SessionName ] = exception;
			page.Response.Redirect( BEGDC.CC.Web.UserControls.WebUtility.Path+"/Workflow/ErrorPopup.aspx?error=" + exception.ErrorNumber);

		}
		public static void CatchPopupError( System.Web.UI.Page page, string errorCode)
		{
			page.Session[ Workflow.Error.SessionName ] = null;
			page.Response.Redirect( BEGDC.CC.Web.UserControls.WebUtility.Path+"/Workflow/ErrorPopup.aspx?error=" + errorCode);
		}

		public static void CatchError( System.Web.UI.Page page, GDCException exception )
		{
//			if( exception.ErrorNumber == Business.QualFlow.ErrorCode.Deny_NonSubmit )
//			{
//				UserControls.WebUtility.ShowAlert( page, Resource.ErrorMessage.Keys.GetString( exception.ErrorNumber.Replace(".","_") ));
//				return;
//			}

			page.Session[ Workflow.Error.SessionName ] = exception;
			page.Response.Redirect( BEGDC.CC.Web.UserControls.WebUtility.Path+"/Workflow/Error.aspx?error=" + exception.ErrorNumber);
		}
		public static void CatchError( System.Web.UI.Page page, string errorCode)
		{
//			if( errorCode == Business.QualFlow.ErrorCode.Deny_NonSubmit )
//			{
//				UserControls.WebUtility.ShowAlert( page, Resource.ErrorMessage.Keys.GetString( errorCode.Replace(".","_") ));
//				return;
//			}

			page.Session[ Workflow.Error.SessionName ] = null;
			page.Response.Redirect( BEGDC.CC.Web.UserControls.WebUtility.Path+"/Workflow/Error.aspx?error=" + errorCode);
		}

	}	
}
